<?php 
include "method/win.php";

if(!isset($_SESSION['user'])){
    show_msg("请先登录!","login.php");
}
if(isset($_GET['action'])){
    logout();
}
if(isset($_GET['page'])){
    $page=$_GET['page'];
}else{
    $page=1;
}
if(isset($_GET['id'])){
    $defsql="select * from weibo_user where user_id='".$_GET['id']."'";
    $defres=$conn->query($defsql);
    $defrow=$defres->fetch_assoc();
}else {
    $defsql="select * from weibo_user where user_name='".$_SESSION['user']."'";
    $defres=$conn->query($defsql);
    $defrow=$defres->fetch_assoc();
}
if($defrow['user_name']==$_SESSION['user']){
    $user = 1;
}else {
    $user = 0;
}

$gzsql="select * from weibo_relation where fans='".$defrow['user_name']."'";
$gzres=$conn->query($gzsql);

$fansql="select * from weibo_relation where user='".$defrow['user_name']."'";
$fanres=$conn->query($fansql);

$sqlsql = "select * from weibo_relation where user='".$defrow['user_name']."' and fans='".$_SESSION['user']."'";
$resres = $conn->query($sqlsql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人信息关注、粉丝管理</title>
    <link rel="stylesheet" href="../lib/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/relation.css">
    <script src="../lib/jquery/jquery.js" type="text/javascript"></script>
    <script src="../lib/bootstrap/js/bootstrap.js" type="text/javascript"></script>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!--头部logo-->
        <div class="navbar-header">
            <a class="navbar-brand" href="#"><em><img src="../img/logo.jpg" height=30 width=30></em><em>小狗微博</em></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!--搜索框-->
            <form class="navbar-form navbar-left" role="search" method="post" action="search_loginafter.php">
                <div class="form-group">
                    <input type="text" class="form-control" style="width: 400px" placeholder="Search" name="str-search">
                </div>
                <button type="sumbit" class="btn btn-default" name="search-btn">搜索</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="index_loginafter.php">首页</a></li>
                <li><a href="person_loginafter.php"><?php echo $_SESSION['user']?></a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">我的消息 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="inbox.php?page=1">@我</a></li>
                        <li><a href="inbox.php?page=2">评论</a></li>
                        <li><a href="inbox.php?page=3">回复</a></li>
                        <li><a href="inbox.php?page=4">赞</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">个人中心 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                    	<li><a href="fixpw.php">修改密码</a></li>
                        <li><a href="fixtx.php">修改头像</a></li>
                        <li><a href="index_loginafter.php?action=logout">退出登录</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!--个人版心-->
<div class="person-warp">
    <!--个人展示框-->
    <div class="person-box">
        <!--个人头像-->
        <div class="person-tx">
            <img src="../<?php echo $defrow['user_tx']?>" style="width: 100px; height: 100px; border-radius: 50%">
        </div>
        <!--用户名-->
        <div class="person-uname">
            <a href="person_loginafter.php?id=<?php echo $defrow['user_id']?>"><?php echo $defrow['user_name']?></a>
        </div>
        <!--关注按钮-->
        <div class="gz-op choose-2">
            <button class="gz-btn" onclick="gzUser('<?php echo $defrow['user_name']?>')"><em class="glyphicon glyphicon-plus"></em><em>关注</em></button>
        </div>
    </div>
    <!--个人内容-->
    <div class="person-content clearfix">
        <!--内容左边-->
        <div class="left-content">
            <!--用户关系-->
            <div class="person-relation clearfix">
                <a href="relation.php?id=<?php echo $defrow['user_id']?>&page=1" id="nav-1" class="choose-5"><em class="glyphicon glyphicon-user"></em>&nbsp;<span>关注</span>&nbsp;<em><?php echo $gzres->num_rows?></em></a>
                <a href="relation.php?id=<?php echo $defrow['user_id']?>&page=2" id="nav-2"><em class="glyphicon glyphicon-user"></em>&nbsp;<span>粉丝</span>&nbsp;<em><?php echo $fanres->num_rows?></em></a>
            </div>
        </div>

        <!--内容右边版心-->
        <div class="right-content clearfix">
            <!--关注-->
            <div class="gz-warp clearfix choose-2" id="gz-warp">
                <!--顶条-->
                <div class="toptab">
                    关注&nbsp;<?php echo $gzres->num_rows?>
                </div>
                <!--用户-->
                <?php
                if($gzres->num_rows){
                    $firstgzrow = $gzres->fetch_assoc();
                    $gzstr = "user_name='".$firstgzrow['user']."' ";
                    while ($othergzrows = $gzres->fetch_assoc()){
                        $gzstr = $gzstr."|| user_name='".$othergzrows['user']."' ";
                    }
                    $sql = "select weibo_user.* from weibo_user where ".$gzstr;
                    $res = $conn->query($sql);
                    
                    while ($row = $res->fetch_assoc()){
                        echo "<div class='user-box clearfix'>";
                        //                 用户头像
                        echo "<div class='user-tx'>";
                        echo "<img src='../".$row['user_tx']."' style='width: 50px; height: 50px; border-radius: 50%'></div>";
                        echo "<div class='user-op'><a href='person_loginafter.php?id=".$row['user_id']."' class='user-link'>".$row['user_name']."</a></div></div>";
                    }
                }else{
                    echo "<div class='gz-box'><span>您还没有关注用户喔</span></div>";;
                }
                
                ?>
            </div>

            <!--粉丝-->
            <div class="fan-warp clearfix" id="fan-warp">
                <!--顶条-->
                <div class="toptab">
                    粉丝&nbsp;<?php echo $fanres->num_rows?>
                </div>
                <!--用户-->
                <?php
                if($fanres->num_rows){
                    $firstfanrow = $fanres->fetch_assoc();
                    $fanstr = "user_name='".$firstfanrow['fans']."' ";
                    while ($otherfanrows = $fanres->fetch_assoc()){
                        $fanstr = $fanstr."|| user_name='".$otherfanrows['user']."' ";
                    }
                    $sql = "select weibo_user.* from weibo_user where ".$fanstr;
                    $res = $conn->query($sql);
                    
                    while ($row = $res->fetch_assoc()){
                        echo "<div class='user-box clearfix'>";
                        //                 用户头像
                        echo "<div class='user-tx'>";
                        echo "<img src='../".$row['user_tx']."' style='width: 50px; height: 50px; border-radius: 50%'></div>";
                        echo "<div class='user-op'><a href='person_loginafter.php?id=".$row['user_id']."' class='user-link'>".$row['user_name']."</a></div></div>";
                    }
                }else{
                    echo "<div class='gz-box'><span>您还没有粉丝喔</span></div>";;
                }
                
                ?>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    if(<?php echo $user?>==1){
        $(".gz-op").removeClass("choose-2");
    }
    if(<?php echo $resres->num_rows?>){
		$(".gz-btn").html("<em>取消关注</em>")
	}

    function gzUser(user){
        var admin = '<?php echo $_SESSION['user']?>';
        if($(".gz-btn").text()=="关注"){
        	$.ajax({
                type:"post",
        		url:"gz_action.php",
        		data:{user: user, admin: admin},
        		success:function(data){
        				alert(data)
        				if(data=="关注成功"){
            				$(".gz-btn").html("<em>取消关注</em>");
        				}
        		}
            });
        }else{
            var caltip = confirm("您确定要取消关注此博主吗？")
            if(caltip){
            	$.ajax({
                    type:"post",
            		url:"calgz_action.php",
            		data:{user: user, admin: admin},
            		success:function(data){
            				alert(data)
            				if(data=="成功取消关注"){
                				$(".gz-btn").html("<em class='glyphicon glyphicon-plus'></em><em>关注</em>");
            				}
            		}
                });
            }
        	
        }
    }

    if(<?php echo $page?>==1){
    	$(".person-relation a").removeClass("choose-5")
        $("#nav-1").addClass("choose-5")
        $(".gz-warp").removeClass("choose-2")
        $(".fan-warp").removeClass("choose-2")
        $(".gz-warp").addClass("choose-2")
    }else{
    	$(".person-relation a").removeClass("choose-5")
        $("#nav-2").addClass("choose-5")
        $(".gz-warp").removeClass("choose-2")
        $(".fan-warp").removeClass("choose-2")
        $(".fan-warp").addClass("choose-2")
    }

//     $(".person-relation a").click(function () {
//         if(this.id=='nav-1'){
//             $(".person-relation a").removeClass("choose-5")
//             $(this).addClass("choose-5")
//             $(".gz-warp").removeClass("choose-2")
//             $(".fan-warp").removeClass("choose-2")
//             $(".gz-warp").addClass("choose-2")
//         }else{
//             $(".person-relation a").removeClass("choose-5")
//             $(this).addClass("choose-5")
//             $(".gz-warp").removeClass("choose-2")
//             $(".fan-warp").removeClass("choose-2")
//             $(".fan-warp").addClass("choose-2")
//         }
//     })
</script>
</html>